package com.arpa.oms.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.controller.BaseController;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.dto.OmsCustomerDTO;
import com.arpa.oms.domain.entity.OmsCustomer;
import com.arpa.oms.domain.vo.OmsCustomerVO;
import com.arpa.oms.service.IOmsCustomerService;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.consts.TipConst;
import com.arpa.wms.domain.vo.CustomerVO;
import com.arpa.wms.domain.vo.WarehouseVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * Oms客户 Controller
 * </p>
 *
 * @author wangdong
 * @since 2021-07-21
 */
@Api(tags = "客户")
@RestController
@RequestMapping(value="/omsCustomer", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsCustomerController extends BaseController {

   @Autowired
   private IOmsCustomerService iCustomerService;

    /**Goo
     * 获取 客户 列表
     *
     * @param customerDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 客户 列表", notes = "获取 客户 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = CustomerVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(OmsCustomerDTO customerDTO, HttpServletRequest request) {
        try {

            customerDTO.setShipmentCode(UserUtil.getLoginWarehouseCodeOrShipmentCode());
            OmsCustomerVO customerVOSum = iCustomerService.queryListSum(customerDTO);
            List<OmsCustomerVO> customerVOList = iCustomerService.queryList(customerDTO);

            Page returnPage = new Page(customerDTO.getPageNum(),customerDTO.getPageSize());
            returnPage.setRecords(customerVOList);
            returnPage.setTotal(customerVOSum.getTotal());
            returnPage.setSumObject(customerVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }




    /**
     * 添加 客户
     *
     * @param entity Customer
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 客户 ", notes = "添加 客户 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(@RequestBody @Validated(OmsCustomerDTO.Save.class) OmsCustomerDTO entity, HttpServletRequest request) {
        try {
            iCustomerService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取客户详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取客户详情", notes = "获取客户详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "客户code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = OmsCustomer.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result customer(@PathVariable String code, HttpServletRequest request) {
        try {
            OmsCustomerVO entity = iCustomerService.getOneInfo(code);
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改客户
     *
     * @param entity 客户
     * @param request
     * @return
     */
    @ApiOperation(value = "修改客户", notes = "修改客户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@RequestBody @Validated(OmsCustomerDTO.Update.class) OmsCustomerDTO entity,HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error(TipConst.CODE_NOT_EMPTY);
        }
        if(entity.getCreditAmount()==null){
        }else {
            entity.setCreditBalance(entity.getCreditBalance().add(entity.getCreditAmount()));
        }
        int updateNum = iCustomerService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 客户
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 客户 ", notes = "删除 客户 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "/batchdel")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iCustomerService.batchDelete(codes, UserUtil.getCode());
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取 客户下拉列表数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "Oms获取客户下拉列表数据", notes = "获取 客户下拉列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = WarehouseVO.class))
    @NeedLogin
    @GetMapping("/chooseData")
    public Result chooseData(
            HttpServletRequest request) {
        List<OmsCustomer> omsCustomers = iCustomerService.list(new LambdaQueryWrapper<OmsCustomer>()
                .select(OmsCustomer::getCode, OmsCustomer::getName)
                .eq(OmsCustomer::getShipmentCode, UserUtil.getLoginWarehouseCodeOrShipmentCode())
                .eq(OmsCustomer::getStatus, NumConst.NUM_ONE)
        );
        return Result.ok().data(omsCustomers);

    }
 }
